<?php
include('config.php');
$sql1="select id from book_list order by id desc limit 1";
$data=mysqli_query($connect,$sql1);
if (mysqli_num_rows($data)==0) {
    $book_name = $_POST['book_name'];
    $author = $_POST['author'];
    $imgname=$_POST['imgname'];
    $brief = $_POST['brief'];
    $sql2 = "insert into book_list(book_name,img,author,brief,link) values('$book_name','$imgname','$author','$brief','http://text.com/book_detail.php?id=1')";
    $res1 = mysqli_query($connect,$sql2);
    if($res1){
        echo "<script>alert('图书插入成功！');location.href='book_display.php';</script>";
    }else{
        echo "<script>alert('图书插入失败！');</script>";
    }
}else{
    while ($out=mysqli_fetch_array($data)) {
        $id=$out['id']+1;
        $book_name = $_POST['book_name'];
        $author = $_POST['author'];
        $imgname=$_POST['imgname'];
        $brief = $_POST['brief'];
        $sql3 = "insert into book_list(book_name,img,author,brief,link) values('$book_name','$imgname','$author','$brief','http://text.com/book_detail.php?id=$id')";
        $res2 = mysqli_query($connect,$sql3);
        if($res2){
            echo "<script>alert('图书插入成功！');location.href='book_display.php';</script>";
        }else{
            echo "<script>alert('图书插入失败！');</script>";
        }
    }
}
?>